import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

public class GameJPanel  extends JPanel implements KeyListener,Runnable{
    MyTank myTank = null;
    EneryTank  eneryTank = null;
    Vector<EneryTank> enemyTankVector = new Vector();
    Vector<Gold> goldVector = new Vector();
    boolean ff=true;
    GameJPanel(){
        this.myTank=new MyTank(200,300);

        for(int i=0;i<5;i++){
            EneryTank eneryTank=new EneryTank((int)(Math.random()*1000),(int)(Math.random()*700));
            this.enemyTankVector.add(eneryTank);
            Thread thread = new Thread(eneryTank);
            thread.start();
        }
    }

    int cnt=0;

    public void paint(Graphics g){
        super.paint(g);
        g.setColor(Color.BLACK);
        g.fillRect(0,0,1000,900);
        this.drawTank(this.myTank.getX(),this.myTank.getY(),g,this.myTank.getDir(),this.myTank.getKind());

        for(int i=0;i<enemyTankVector.size();i++){
            EneryTank eneryTank = (EneryTank) this.enemyTankVector.get(i);

            while(enemyTankVector.size()<5){
                EneryTank eee=new EneryTank((int)(Math.random()*970),(int)(Math.random()*820));
                if((myTank.getX()>=eneryTank.getX() && myTank.getX()<=eneryTank.getX()+30) && (myTank.getY()>=eneryTank.getY() && myTank.getY()<=eneryTank.getY()+30))continue;
                this.enemyTankVector.add(eee);
                Thread thread = new Thread(eee);
                thread.start();
            }
            this.drawTank(eneryTank.getX(),eneryTank.getY(),g,eneryTank.getDir(),eneryTank.getKind());
        }

        for(int i=0;i<goldVector.size();i++){
            Gold gold = (Gold) this.goldVector.get(i);
            g.setColor(Color.ORANGE);
            g.fillOval(gold.getX(),gold.getY(),15,15);
        }

        for(int i = 0; i<myTank.bulletVector.size();i++){
            Bullet bullet = myTank.bulletVector.get(i);
            check(bullet);
            if(!bullet.isLife()){
                myTank.bulletVector.remove(bullet);
            }
            g.setColor(Color.RED);
            g.fill3DRect(bullet.getX(),bullet.getY(),5,5,false);
        }
        if(true){
            ImageIcon imageIcon = new ImageIcon("C:\\Users\\29734\\IdeaProjects\\untitled1\\src\\规则.png");
            g.drawImage(imageIcon.getImage(), 1000, 0, this);
        }
        if(cnt==0){
            ImageIcon imageIcon = new ImageIcon("C:\\Users\\29734\\IdeaProjects\\untitled1\\src\\0.png");
            g.drawImage(imageIcon.getImage(), 1000, 500, this);
        }
        if(cnt==1){
            ImageIcon imageIcon = new ImageIcon("C:\\Users\\29734\\IdeaProjects\\untitled1\\src\\1.png");
            g.drawImage(imageIcon.getImage(), 1000, 500, this);
        }
        if(cnt==2){
            ImageIcon imageIcon = new ImageIcon("C:\\Users\\29734\\IdeaProjects\\untitled1\\src\\2.png");
            g.drawImage(imageIcon.getImage(), 1000, 500, this);
        }
        if(cnt==3){
            ImageIcon imageIcon = new ImageIcon("C:\\Users\\29734\\IdeaProjects\\untitled1\\src\\3.png");
            g.drawImage(imageIcon.getImage(), 1000, 500, this);
        }
        if(cnt==4){
            ImageIcon imageIcon = new ImageIcon("C:\\Users\\29734\\IdeaProjects\\untitled1\\src\\4.png");
            g.drawImage(imageIcon.getImage(), 1000, 500, this);
        }
        if(cnt==5){
            ImageIcon imageIcon = new ImageIcon("C:\\Users\\29734\\IdeaProjects\\untitled1\\src\\5.png");
            g.drawImage(imageIcon.getImage(), 1000, 500, this);
        }
        if(cnt==6){
            ImageIcon imageIcon = new ImageIcon("C:\\Users\\29734\\IdeaProjects\\untitled1\\src\\6.png");
            g.drawImage(imageIcon.getImage(), 1000, 500, this);
        }
        if(cnt==7){
            ImageIcon imageIcon = new ImageIcon("C:\\Users\\29734\\IdeaProjects\\untitled1\\src\\7.png");
            g.drawImage(imageIcon.getImage(), 1000, 500, this);
        }
        if(cnt==8){
            ImageIcon imageIcon = new ImageIcon("C:\\Users\\29734\\IdeaProjects\\untitled1\\src\\8.png");
            g.drawImage(imageIcon.getImage(), 1000, 500, this);
        }
        if(cnt==9){
            ImageIcon imageIcon = new ImageIcon("C:\\Users\\29734\\IdeaProjects\\untitled1\\src\\8.png");
            g.drawImage(imageIcon.getImage(), 1000, 500, this);
        }
        if(cnt==10){
            ImageIcon imageIcon = new ImageIcon("C:\\Users\\29734\\IdeaProjects\\untitled1\\src\\10.png");
            g.drawImage(imageIcon.getImage(), 1000, 500, this);
        }

        if(cnt==10){
            ImageIcon imageIcon = new ImageIcon("C:\\Users\\29734\\IdeaProjects\\untitled1\\src\\Win.png");
            g.drawImage(imageIcon.getImage(), 200, 100, this);
        }
        check(myTank.getX(),myTank.getY());
        if(ff==false){
            ImageIcon imageIcon = new ImageIcon("C:\\Users\\29734\\IdeaProjects\\untitled1\\src\\Fail.png");
            g.drawImage(imageIcon.getImage(), 200, 100, this);
        }
    }

    public void drawTank(int x, int y,Graphics g,int dir,int kind){
        if(kind==1){
            if(dir==0){
                g.setColor(Color.RED);
                g.fill3DRect(x,y,30,30,true);
                g.fill3DRect(x-3,y-3,5,40,true);
                g.fill3DRect(x+30,y-3,5,40,true);
                g.setColor(Color.PINK);
                g.fillOval(x+7,y+7,14,14);
                g.drawLine(x+14,y+7,x+14,y-20);
            }else if(dir==1){
                g.setColor(Color.RED);
                g.fill3DRect(x,y,30,30,true);
                g.fill3DRect(x-3,y-3,5,40,true);
                g.fill3DRect(x+30,y-3,5,40,true);
                g.setColor(Color.PINK);
                g.fillOval(x+7,y+7,14,14);
                g.drawLine(x+14,y+7,x+14,y+47);
            }else if(dir==2){
                g.setColor(Color.RED);
                g.fill3DRect(x,y,30,30,true);
                g.fill3DRect(x-3,y-3,40,5,true);
                g.fill3DRect(x-3,y+30,40,5,true);
                g.setColor(Color.PINK);
                g.fillOval(x+7,y+7,14,14);
                g.drawLine(x+7,y+14,x-20,y+14);
            }else if(dir==3){
                g.setColor(Color.RED);
                g.fill3DRect(x,y,30,30,true);
                g.fill3DRect(x-3,y-3,40,5,true);
                g.fill3DRect(x-3,y+30,40,5,true);
                g.setColor(Color.PINK);
                g.fillOval(x+7,y+7,14,14);
                g.drawLine(x+14,y+14,x+47,y+14);
            }
        }else if(kind==2){
            if(dir==0){
                g.setColor(Color.BLUE);
                g.fill3DRect(x,y,30,30,true);
                g.fill3DRect(x-3,y-3,5,40,true);
                g.fill3DRect(x+30,y-3,5,40,true);
                g.setColor(Color.CYAN);
                g.fillOval(x+7,y+7,14,14);
                g.drawLine(x+14,y+7,x+14,y-20);
            }else if(dir==1){
                g.setColor(Color.BLUE);
                g.fill3DRect(x,y,30,30,true);
                g.fill3DRect(x-3,y-3,5,40,true);
                g.fill3DRect(x+30,y-3,5,40,true);
                g.setColor(Color.CYAN);
                g.fillOval(x+7,y+7,14,14);
                g.drawLine(x+14,y+7,x+14,y+47);
            }else if(dir==2){
                g.setColor(Color.BLUE);
                g.fill3DRect(x,y,30,30,true);
                g.fill3DRect(x-3,y-3,40,5,true);
                g.fill3DRect(x-3,y+30,40,5,true);
                g.setColor(Color.CYAN);
                g.fillOval(x+7,y+7,14,14);
                g.drawLine(x+7,y+14,x-20,y+14);
            }else if(dir==3){
                g.setColor(Color.BLUE);
                g.fill3DRect(x,y,30,30,true);
                g.fill3DRect(x-3,y-3,40,5,true);
                g.fill3DRect(x-3,y+30,40,5,true);
                g.setColor(Color.CYAN);
                g.fillOval(x+7,y+7,14,14);
                g.drawLine(x+14,y+14,x+47,y+14);
            }
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }
    @Override
    public void keyPressed(KeyEvent e) {
        int key=e.getKeyCode();
        if(key==KeyEvent.VK_UP){
            this.myTank.setDir(0);
            this.myTank.up();
        }else if(key==KeyEvent.VK_DOWN){
            this.myTank.setDir(1);
            this.myTank.down();
        }else if(key==KeyEvent.VK_LEFT){
            this.myTank.setDir(2);
            this.myTank.left();
        }else if(key==KeyEvent.VK_RIGHT){
            this.myTank.setDir(3);
            this.myTank.right();
        }
        check();
        if(key==KeyEvent.VK_SPACE){
            if(myTank.bulletVector.size()<30){
                myTank.attack();
            }
        }
        this.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void run(){
        while(true){
            try{
                Thread.sleep(50);
            }catch (InterruptedException e){
                throw new RuntimeException((e));
            }

            if (cnt==10 || ff==false){
                break;
            }
            this.repaint();
        }

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.exit(0);
    }

    public void check(Bullet bullet){
        if(bullet.getY()<5 || bullet.getY()>890 || bullet.getX()<5 || bullet.getX()>990){
            bullet.setLife(false);
        }
        for(int i=0;i<enemyTankVector.size();i++){
            EneryTank eneryTank = (EneryTank) this.enemyTankVector.get(i);
            if((bullet.getX()>=eneryTank.getX() && bullet.getX()<=eneryTank.getX()+30) && (bullet.getY()>=eneryTank.getY() && bullet.getY()<=eneryTank.getY()+30)){
                bullet.setLife(false);
                Gold gold = new Gold(eneryTank.getX(),eneryTank.getY());
                goldVector.add(gold);
                enemyTankVector.remove(eneryTank);
                break;
            }
        }
    }

    public void check(){
        for(int i=0;i<goldVector.size();i++){
            Gold gold1 = (Gold) goldVector.get(i);
            if(gold1.getX()>=myTank.getX()-3 && gold1.getX()<=myTank.getX()+33 && gold1.getY()>=myTank.getY()-3 && gold1.getY()<=myTank.getY()+33){
                goldVector.remove(gold1);
                cnt++;
            }
        }
    }

    public void check(int x,int y){
        for(int i=0;i<enemyTankVector.size();i++){
            EneryTank eneryTank = (EneryTank) this.enemyTankVector.get(i);
            if(((x>=eneryTank.getX() && x<=eneryTank.getX()+30) && (y>=eneryTank.getY() && y<=eneryTank.getY()+30))||((x+30>=eneryTank.getX() && x+30<=eneryTank.getX()+30) && (y+30>=eneryTank.getY() && y+30<=eneryTank.getY()+30))){
                ff=false;
            }
        }
    }


}
